package tmp;

import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import br.gendi.beans.Parametros;

import au.id.jericho.lib.html.FormField;
import au.id.jericho.lib.html.FormFields;
import au.id.jericho.lib.html.Source;
import au.id.jericho.lib.html.Tag;

public class FormFieldList {
	public static void main(String[] args) throws Exception {
		String sourceUrlString="data/form.html";
		if (sourceUrlString.indexOf(':')==-1) 
			sourceUrlString="file:"+sourceUrlString;
		
		Source source=new Source(new URL(sourceUrlString));
		FormFields formFields=source.findFormFields();
		//System.out.println("The document "+sourceUrlString+" contains "+formFields.size()+" form fields:\n");
		
		//System.out.println(source.findAllElements(Tag.FORM));
		{
			List l = source.findAllElements(Tag.FORM);
			//System.out.println(l.si);
			
			//action
			String form = l.toString();
			int inicioAction = form.indexOf("action=\"") +  new String("action=\"").length();
			int fimAction = form.indexOf('"', inicioAction);
			String action = form.substring(inicioAction, fimAction);
	
			System.out.println(inicioAction + "," + fimAction + " ---> "+action);
			
			//method
			int inicioMethod = form.indexOf("method=\"") +  new String("method=\"").length();
			int fimMethod = form.indexOf('"', inicioMethod);
			String method = form.substring(inicioMethod, fimMethod);
	
			System.out.println(inicioMethod + "," + fimMethod + " ---> "+method);
			
		}
		System.out.println("---------------------------------");
		ArrayList<Parametros> array = new ArrayList<Parametros>();
		for (Iterator i=formFields.iterator(); i.hasNext();) {
			FormField formField=(FormField)i.next();
			
			Parametros p = new Parametros();
			p.setNome(formField.getName());
			p.setValores(formField.getPredefinedValues());
			array.add(p);
	
			
			//Collection<String> col =  formField.getPredefinedValues();
			System.out.println(formField.getName() + " --> " + formField.getValues());
			//System.out.println(formField.getDebugInfo());
		}
	}
}

